import { donors } from "../../database/init/donor/donorStatic";
import { getDonor, addDonor, getData } from './function.js';

const db = wx.cloud.database();
const _ = db.command;

var wxCharts = require("../../utils/wxcharts.js");
Page({

  data: {
    donorInfo: {},
    sept: '',
    oct: '',
    nov: '',
    dec: '',
    totalMoney: '',

    value: '',
    active: 2,
    money: 25555,
    times: 44,
    imageWidth: 0,
    info: {
      name: '刘先生',
      gender: '男',
      age: '48',
      birthday: '1月1日',
      residence: '辽宁大连',
      from: '浙江杭州'
    },
    // first_child: {
    //   has: true,
    //   id: 1,
    //   name: '李四',
    //   location: '贵州',
    //   introduce: '留守儿童 成绩优异',
    //   picture: 'https://img.yzcdn.cn/vant/cat.jpeg'
    // },
    // second_child: {
    //   has: true,
    //   id: 2,
    //   name: '张三',
    //   location: '贵州',
    //   introduce: '留守儿童 成绩优异',
    //   picture: 'https://img.yzcdn.cn/vant/cat.jpeg'
    // },
    love: 484,

    // new
    donatedChildren: []
  },

  async init() {
    var res = await getData("ourOx68K-yzP1JGUbwnM9FAEJ8hA");
    console.log(res);
    this.setData({
      childList: res.students.slice(0, 2),
      // first_child: res.students.slice(0, 2)[0],
      // second_child: res.students.slice(0, 2)[1],
      donorInfo: res,
      sept: res.totalDonation.sept,
      oct: res.totalDonation.oct,
      nov: res.totalDonation.nov,
      dec: res.totalDonation.dec,
      totalMoney: res.totalDonation.sept + res.totalDonation.oct + res.totalDonation.nov + res.totalDonation.dec,
    })

    this.setData({
      first_child: this.data.childList[0],
      second_child: this.data.childList[1],
    })

    console.log(this.data.first_child.money);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },


  /**
  * 生命周期函数--监听页面显示
  */
  async onShow() {
    this.init();

    let windowHeight = 900;
    let windowWidth = 320;
    try {
      let res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
      windowHeight = res.windowHeight;
    } catch (e) {
    }
    let chartCategories = ['9月', '10月', '11月', '12月']
    var res = await getData("ourOx68K-yzP1JGUbwnM9FAEJ8hA");
    let chartData = [res.totalDonation.sept, res.totalDonation.oct, res.totalDonation.nov, res.totalDonation.dec]

    new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      categories: chartCategories,
      series: [{
        name: '捐款统计',
        data: chartData
      }],
      yAxis: {
        format: function (val) {
          return val;
        },
        min: 0
      },
      width: 0.9 * windowWidth,
      height: 0.29 * windowHeight
    });
  }
})
